//
//  Extension+NSAttributedString.swift
//  WeiBo
//
//  Created by 韩菲菲 on 16/8/21.
//  Copyright © 2016年 韩菲菲. All rights reserved.
//

import UIKit

enum WBAttachmentType:String {
    case WBTextType
    case NSTextType
}



extension NSAttributedString{
    
    
    
    //根据文字附件 返回 一个富文本
    class func getAttributedStringWithEmoticonModel(emoticon:WBEmoticonModel , textSize: CGFloat , y: CGFloat , type: WBAttachmentType) -> NSAttributedString{
        
        //根据模型取出图片
        let image = UIImage.emoticonWithImage(emoticon)
        
        
        if type == .WBTextType{
            
            //创建一个文字附件
            let attach = WBTextAttachment()
            
            //设置模型属性
            attach.emoticon = emoticon
            
            setupAttachment(attach, image: image, textSize: textSize, y: y)
            
            //创建一个不可变的富文本
            return NSAttributedString(attachment: attach)
            
        }else
        {
            
            //创建一个文字附件
            let attach = NSTextAttachment()
            
            setupAttachment(attach, image: image, textSize: textSize, y: y)
            
            //创建一个不可变的富文本
            return NSAttributedString(attachment: attach)
            
            
        }
        
    }
    
    class func setupAttachment(attach: NSTextAttachment , image: UIImage? , textSize: CGFloat , y: CGFloat) {
        
        //设置图片属性
        attach.image = image
        
        //获取行高
        let lineHeight = UIFont.systemFontOfSize(textSize).lineHeight
        
        attach.bounds = CGRect(x:0, y: y, width: lineHeight, height: lineHeight)
        
        
    }
    
    
    
}
